$(function() {
    function send(url) {
        let arr=[];
        $.ajax({
            // method:"GET",
            type:"get",
            async:false,
            url,
            dataType:"json",
            success:function(data) {
                arr = data;
            }
        })
        return arr;
    }
    function upLeftData(Arr) {
        let str='';
        for (let key in Arr) {
         str +="<li><ul><li><input type='checkbox' id='"+Arr[key].id +"'/></li><li>"+ Arr[key].SN+"</li><li>"+Arr[key].room +"</li></ul></li>"
        }
        $(".list1").html(str);
    }
        function upRightData(Arr) {
        let str='';
        for (let key in Arr) {
         str +="<li><ul><li><input type='checkbox' id='"+Arr[key].id +"'/></li><li>"+ Arr[key].SN+"</li><li>"+ Arr[key].time+"</li><li>"+Arr[key].room +"</li></ul></li>"
        }
        $(".list2").html(str);
    }
     
    let leftArr = [];
    let rightArr = [];
    let temArr1 = [];
    let temArr2 = [];
    let selectvalue = $("#selectValue").children("option:selected").val();
    let keyL= '';
    let keyR = '';
    let data = send('./control.json')
    leftArr = data;
    upLeftData(leftArr);
    $(document).on("change",".list1 input:checkbox",function() {
        keyL = $(this).prop("id");
        if($(this).prop("checked") == true) {
            temArr1[keyL] = leftArr[keyL];
        } else{
            leftArr[keyL] = temArr1[keyL];
            delete temArr1[keyL];
        }
    } )
    $("#selectValue").on("change",function() {
        selectvalue = $(this).children("option:selected").val();
        if(!temArr1.length){
            return ;
        }        
    })
    $(".btn1").on("click",function() {
        for (let key in temArr1) {
            delete leftArr[key];
            temArr1[key].time = selectvalue;
            rightArr[key]= temArr1[key];
        }
            temArr1 = [];//清空临时数组temArr1
            upLeftData(leftArr);
            upRightData(rightArr);
    })
    $(document).on("change",".list2 input:checkbox",function() {
        keyR = $(this).prop("id");
        if($(this).prop("checked") == true) {
            temArr2[keyR] = rightArr[keyR];
        } else{
            rightArr[keyR] = temArr2[keyR];
            delete temArr2[keyR];
        }
    })
    
    $(".btn2").on("click",function() {
        for(let key in temArr2) {
            delete rightArr[key];
            leftArr[key] = temArr2[key];
        }
            temArr2 = [];//
            upLeftData(leftArr);
            upRightData(rightArr);
    })
})